package com.drojian.screenAdaptation2;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.drojian.demo.R;

public class PercentRelativelayout extends RelativeLayout {
    public PercentRelativelayout(Context context) {
        super(context);
    }

    public PercentRelativelayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PercentRelativelayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public PercentRelativelayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        context.getResources().getDimensionPixelSize(R.dimen.buttonHeight);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int childCount = this.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = this.getChildAt(i);
            ViewGroup.LayoutParams layoutParams = child.getLayoutParams();
            //把已经得到的布局参数进行修改
            float widthPercent = 0;
            float heightPercent = 0;
            if(layoutParams instanceof PercentRelativelayout.LayoutParams){
                widthPercent = ((LayoutParams)layoutParams).getWidthPercent();
                heightPercent = ((LayoutParams)layoutParams).getHeightPercent();
            }
            if(widthPercent > 0){
                layoutParams.width = (int)(width * widthPercent);
                layoutParams.height = (int)(height * heightPercent);
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }
    //把需要的自定义属性封装进去
    @Override
    public RelativeLayout.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParams(getContext(),attrs);
    }

    public static class LayoutParams extends RelativeLayout.LayoutParams{
        private float widthPercent;
        private float heightPercent;

        public float getWidthPercent() {
            return widthPercent;
        }

        public void setWidthPercent(float widthPercent) {
            this.widthPercent = widthPercent;
        }

        public float getHeightPercent() {
            return heightPercent;
        }

        public void setHeightPercent(float heightPercent) {
            this.heightPercent = heightPercent;
        }

        /**
         *  在这里把自定义属性加进去
         * @param c
         * @param attrs
         */
        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
            TypedArray array = c.obtainStyledAttributes(attrs, R.styleable.PercentRelativelayout);
            widthPercent = array.getFloat(R.styleable.PercentRelativelayout_layout_widthPercent,this.getWidthPercent());
            heightPercent = array.getFloat(R.styleable.PercentRelativelayout_layout_heightPercent,this.getHeightPercent());
        }

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(ViewGroup.LayoutParams source) {
            super(source);
        }
    }
}
